package extraction.html.chase;

import org.htmlparser.Parser;
import org.htmlparser.Node;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.NodeList;

import java.util.*;
import java.sql.Date;

import extraction.html.ExtractionUtils;

/**
 * @author Nathan Petryk (nathanp@indeed.com)
 */
public class ChaseTransactionScreenParser {

    private ChaseTransactionScreenParser() {}


    public static AccountTransaction[] extractTransactions(String in) throws ParserException {
        Parser parse = new Parser(in);
        NodeList nodes = parse.parse(null);
        
        Node table;
        Node[] tableRows;

        List<Node> temp = new ArrayList<Node>();
        ExtractionUtils.findWith(temp, "id=\"TransactionDetailsCtrl_TransactionDetailsGrid\"", nodes.toNodeArray());
        table = temp.get(0);
        temp.clear();

        ExtractionUtils.getNodesOfType("tr", 1, table, temp);
        tableRows = temp.toArray(new Node[0]);
        tableRows = Arrays.copyOfRange(tableRows, 1, tableRows.length);

        AccountTransaction[] rtn = new AccountTransaction[tableRows.length];

        int i = 0;
        for(Node n : tableRows) {            
            rtn[i++] = parseTransaction(n);
        }

        return rtn;
    }

    private static AccountTransaction parseTransaction(Node row) {

        List<Node> elements = new ArrayList<Node>();
        ExtractionUtils.getNodesOfType("td", 1, row, elements);


        String date = elements.get(0).getFirstChild().getText().replaceAll("&nbsp;", "");
        String type = elements.get(1).getFirstChild().getText().replaceAll("&nbsp;", "");
        String desc = elements.get(2).getFirstChild().getText().replaceAll("&nbsp;", "").replaceAll("[\\s]{2,}", " ");
        String debi = elements.get(3).getFirstChild().getText().replaceAll("&nbsp;", "").replaceAll("[^0-9\\.\\+\\-]", "");
        String cred = elements.get(4).getFirstChild().getText().replaceAll("&nbsp;", "").replaceAll("[^0-9\\.\\+\\-]", "");
        String bala = elements.get(5).getFirstChild().getText().replaceAll("&nbsp;", "").replaceAll("[^0-9\\.\\+\\-]", "");

        int[] dateElements = getDateElements(date);
        Calendar calObj = new GregorianCalendar();
        calObj.set(dateElements[2], dateElements[0], dateElements[1]);
        Date dateObj = new Date(calObj.getTimeInMillis());


        System.out.println(dateElements[2]+", "+dateElements[1]+", "+dateElements[0]+" -- "+dateObj);

        AccountTransaction trans = new AccountTransaction(
                dateObj,
                type,
                desc,
                (debi.length() > 0 ? Double.parseDouble(debi) : 0),
                (cred.length() > 0 ? Double.parseDouble(cred) : 0),
                (bala.length() > 0 ? Double.parseDouble(bala) : 0)
        );
        /*
        System.out.println();
        System.out.printf("%-10s %-22s %-50s | %4.2f, %4.2f, %4.2f", trans.date, trans.type, trans.description, trans.debit, trans.credit, trans.balance );
          */
        return trans;    
    }

    private static int[] getDateElements(String str) {
        String[] components = str.split("[^0-9]");
        int[] rtn = new int[3];

        int i = 0;
        for(String s : components) {
            rtn[i++] = Integer.parseInt(s);
        }

        return rtn;
    }

    public static void main(String[] args) throws ParserException {
        ChaseTransactionScreenParser.extractTransactions("//home//nathanp//jtml//sample//trans.html");

    }
}
